﻿/*======================================*\
* Author: Vincent
* LastModifyUser: Vincent
* Description:  
* CreateTime： 2012-05
* Remark:  
\*======================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppCode.DbDTO;
using AppCode.Util;

namespace AppCode.Domain.Services
{
    /// <summary>
    /// 属性处理部分
    /// <para>
    /// 针对模板
    /// </para>
    /// </summary>
    public class CodeCreatePropertyHandler
    {

        /// <summary>
        /// 创建标记列
        /// </summary>
        /// <returns></returns>
        public string ColumnTagStr(TableInfo item)
        {
            string _columnName = item.ColumnName.ToLower(); //小写字段名
            string columnName = item.ColumnName;
            string dataType = item.ColumnType;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} /// <summary> \r\n",InputSpace(2));
            sb.AppendFormat("{1} /// {0} \r\n", item.ColumnRemark,InputSpace(2));
            sb.AppendFormat("{0}/// <para>自增列</para> \r\n", InputSpace(2));
            sb.AppendFormat("{0}/// </summary> \r\n", InputSpace(2));
            sb.AppendFormat("\t\t[ColumnProperty(IsAutoAdd = true, ColumnName = \"{0}\")]\r\n", columnName);
            sb.AppendFormat("\t\t public {0} {1}\r\n", dataType, columnName);
            sb.Append(InputSpace(2)+" { \r\n");
            sb.AppendFormat("\t\t\tset \r\n");
            sb.Append(InputSpace(3) + " { \r\n");
            sb.AppendFormat("{1}object oldValue = _{0};\r\n", _columnName, InputSpace(5));
            sb.AppendFormat("{1}_{0} = value ;\r\n", _columnName, InputSpace(5));
            sb.AppendFormat("{2}this.OnPropertyChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(5));
            sb.Append(InputSpace(3) + " } \r\n");
            sb.Append(InputSpace(3)+"get{ return _"+_columnName+";} \r\n  ");
            sb.Append(InputSpace(2) + " } \r\n");
            sb.AppendLine("");
            return sb.ToString();
        }

        /// <summary>
        /// 创建主键标记列
        /// </summary>
        /// <returns></returns>
        public string ColumnPrimaryKeyStr(TableInfo item)
        {
            string _columnName = item.ColumnName.ToLower(); //小写字段名
            string columnName = item.ColumnName;
            string dataType = item.ColumnType;
            string isAutoAdd = string.Format("{0}", (item.IsIdentity == "1" ? " IsAutoAdd =true, " : ""));

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} /// <summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1} /// {0} \r\n", item.ColumnRemark, InputSpace(2));
            sb.AppendFormat("{0}/// <para>主键列</para> \r\n", InputSpace(2));
            sb.AppendFormat("{0}/// </summary> \r\n", InputSpace(2));
            sb.AppendFormat("\t\t[ColumnProperty({1}ColumnName = \"{0}\") ]\r\n", columnName,isAutoAdd);
            sb.AppendFormat("\t\t public {0} {1}\r\n", dataType, columnName);
            sb.Append(InputSpace(2) + " { \r\n");
            sb.AppendFormat("\t\t\tset \r\n");
            sb.Append(InputSpace(3) + " { \r\n");
            sb.AppendFormat("{1}object oldValue = _{0};\r\n", _columnName, InputSpace(4));
            sb.AppendFormat("{1}_{0} = value ;\r\n", _columnName, InputSpace(4));
            sb.AppendFormat("{2}this.OnPropertyChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(4));
            sb.Append(InputSpace(3) + " } \r\n");
            sb.Append(InputSpace(3) + "get{ return _" + _columnName + ";} \r\n  ");
            sb.Append(InputSpace(2) + " } \r\n");
            sb.AppendLine("");
            return sb.ToString();
        }

        /// <summary>
        /// 创建特殊类型
        /// <para>
        /// 包含int,bool,float,double,decimal等即时映射的类型
        /// </para>
        /// </summary>
        /// <returns></returns>
        public string ColumnSpecialTypeStr(TableInfo item)
        {
            string _columnName = item.ColumnName.ToLower(); //小写字段名
            string columnName = item.ColumnName;
            string dataType = item.ColumnType;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} /// <summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1} /// {0} \r\n", item.ColumnRemark, InputSpace(2));
            sb.AppendFormat("{0}/// </summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1}[ColumnProperty(ColumnName = \"{0}\")]\r\n", columnName, InputSpace(2));
            sb.AppendFormat("{2}public {0} {1}\r\n", dataType, columnName, InputSpace(2));

            sb.Append(InputSpace(2)+"{ \r\n");

            sb.AppendFormat("{0}set \r\n", InputSpace(3));
            sb.Append(InputSpace(3)+"{  \r\n");
            sb.AppendFormat("{1}object oldValue = _{0};\r\n", _columnName, InputSpace(4));
            sb.AppendFormat("{1}_{0} = value ;\r\n", _columnName, InputSpace(4));

            sb.AppendFormat("{2}this.OnPropertyChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(4));
            sb.AppendFormat("{2}this.OnPropertyMappingChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(4));

            sb.Append(InputSpace(3)+"} \r\n ");
            sb.Append(InputSpace(3)+"get{ return _"+_columnName+";} \r\n  ");
            sb.Append(InputSpace(2)+" } \r\n");
            sb.AppendLine("");
            return sb.ToString();
        }


        /// <summary>
        /// 创建公用字段信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public string ColumnPublicStr(TableInfo item)
        {
            string _columnName = item.ColumnName.ToLower(); //小写字段名
            string columnName = item.ColumnName;
            string dataType = item.ColumnType;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} /// <summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1} /// {0} \r\n", item.ColumnRemark, InputSpace(2));
            sb.AppendFormat("{0}/// </summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1}[ColumnProperty(ColumnName = \"{0}\")]\r\n", columnName, InputSpace(2));
            sb.AppendFormat("{2}public {0} {1}\r\n", dataType, columnName, InputSpace(2));
            sb.Append(InputSpace(2) + "{ \r\n");
            sb.AppendFormat("{0}set \r\n", InputSpace(3));
            sb.Append(InputSpace(3) + "{  \r\n");
            sb.AppendFormat("{1}object oldValue = _{0};\r\n", _columnName, InputSpace(4));
            sb.AppendFormat("{1}_{0} = value ;\r\n", _columnName, InputSpace(4));

            sb.AppendFormat("{0}if ((value == null) || (!value.Equals(oldValue))) \r\n", InputSpace(4));
            sb.Append(InputSpace(4) + "{  \r\n");

            sb.AppendFormat("{2}this.OnPropertyChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(5));
            sb.AppendFormat("{2}this.OnPropertyMappingChanged(\"{0}\", _{1}, oldValue);\r\n", columnName, _columnName, InputSpace(5));

            sb.Append(InputSpace(4) + "}  \r\n");

            sb.Append(InputSpace(3) + "} \r\n ");
            sb.Append(InputSpace(3) + "get{ return _" + _columnName + ";} \r\n  ");
            sb.Append(InputSpace(2) + " } \r\n");
            sb.AppendLine("");
            return sb.ToString();

        }

        /// <summary>
        /// 创建公用字段信息
        /// <remarks>
        /// 只有get,set
        /// </remarks>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public string ColumnPublicStrAccess(TableInfo item)
        {
            string _columnName = item.ColumnName.ToLower(); //小写字段名
            string columnName = item.ColumnName;
            string dataType = SqlTypeToCshare.ChangeToCSharpType(item.ColumnType, item.IsAlowNull);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} /// <summary> \r\n", InputSpace(2));
            sb.AppendFormat("{1} /// {0} \r\n", item.ColumnRemark, InputSpace(2));
            sb.AppendFormat("{0}/// </summary> \r\n", InputSpace(2));
            sb.AppendFormat("{2}public {0} {1}\r\n", dataType, columnName, InputSpace(2));
            sb.Append(InputSpace(2) + "{ \r\n");
            sb.AppendFormat("{0}get; \r\n", InputSpace(3));
            
            sb.Append(InputSpace(3) + "set;\r\n}   ");
            sb.Append(InputSpace(2) + " } \r\n");
            sb.AppendLine("");
            return sb.ToString();

        }

        /// <summary>
        /// 输入空格
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string InputSpace(int count)
        {
            string str = string.Empty;
            for (int i = 0; i < count; i++)
            {
                str += "\t";
            }
            return str;
        }

    }
}
